package com.leon.datalink.web.testcontroller;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.leon.datalink.core.utils.JacksonUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Random;

/**
 * Rule management controller for development - simplified version without complex dependencies
 */
@RestController
@RequestMapping("/api/rule")
@CrossOrigin(origins = "*", maxAge = 3600)
public class RuleManagementController {

    private final Random random = new Random();

    @PostMapping("/list")
    public Object listRules(@RequestBody(required = false) Object rule) {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "success");
        
        ArrayNode data = JacksonUtils.createEmptyArrayNode();
        
        // 模拟规则列表数据
        for (int i = 1; i <= 5; i++) {
            ObjectNode ruleItem = JacksonUtils.createEmptyJsonNode();
            ruleItem.put("ruleId", "rule_" + i);
            ruleItem.put("ruleName", "示例规则 " + i);
            ruleItem.put("description", "这是第" + i + "个示例规则");
            ruleItem.put("enable", random.nextBoolean());
            ruleItem.put("nodeCount", random.nextInt(10) + 1);
            ruleItem.put("updateTime", LocalDateTime.now().minusDays(i).toString());
            ruleItem.put("createTime", LocalDateTime.now().minusDays(i + 10).toString());
            data.add(ruleItem);
        }
        
        result.set("data", data);
        return result;
    }

    @GetMapping("/info")
    public Object getRuleInfo(@RequestParam("ruleId") String ruleId) {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "success");
        
        ObjectNode data = JacksonUtils.createEmptyJsonNode();
        data.put("ruleId", ruleId);
        data.put("ruleName", "示例规则");
        data.put("description", "这是一个示例规则");
        data.put("enable", true);
        data.put("nodeCount", 3);
        data.put("graphJson", "{}");
        data.put("updateTime", LocalDateTime.now().toString());
        
        result.set("data", data);
        return result;
    }

    @PostMapping("/add")
    public Object addRule(@RequestBody Object rule) {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "规则添加成功");
        
        ObjectNode data = JacksonUtils.createEmptyJsonNode();
        data.put("ruleId", "rule_" + System.currentTimeMillis());
        
        result.set("data", data);
        return result;
    }

    @PostMapping("/remove")
    public Object removeRule(@RequestBody Object rule) {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "规则删除成功");
        return result;
    }

    @PutMapping("/update")
    public Object updateRule(@RequestBody Object rule) {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "规则更新成功");
        return result;
    }

    @PostMapping("/start")
    public Object startRule(@RequestBody Object rule) {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "规则启动成功");
        return result;
    }

    @PostMapping("/stop")
    public Object stopRule(@RequestBody Object rule) {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "规则停止成功");
        return result;
    }
}
